import pandas as pd
import numpy as np

import os


class TrainData:
    
    # Training Data File Name wtih extern
    __trainFilePath = ""
    __testFilePath = ""
    
    ## 数据起始列号
    __startIndex = 1
    ## 输入数据列数
    __inNum = 3
    ## 输入与输出间隔列数
    __outSpacendex = 0
    ## 输出数据列数
    __outNum = 4
    
    ## 数据格式
    __trainSet = pd.DataFrame()
    __testSet = pd.DataFrame()
        
    # Constructor to define csv file Path
    def __init__(self, filePath):
        ## Get File path
        self.__trainFilePath = filePath
        self.__testFilePath = filePath
        ## Read File
        self.__setDataFrame()
        
    def SetTestFilePath(self, testFilePath):
        ## Get File path
        self.__testFilePath = testFilePath
        ## Read File
        self.__setDataFrame()
    
    # Set Read CSV File parameters
    # startIndex_:      数据起始列号
    # inNum_:           输入数据列数
    # outSpacendex_:    输入与输出间隔列数
    # outNum_:          输出数据列数
    def SetReadFileParams(self, startIndex_, inNum_, outSpacendex_, outNum_):
        ## 获得属性
        self.__startIndex = startIndex_
        self.__inNum = inNum_
        self.__outSpacendex = outSpacendex_
        self.__outNum = outNum_
     
    # Get Traning Values
    # isInput:      is input or output values
    def GetTrainValues(self, isInput):
        return self.__getValues(True, isInput);
    
    # Get test Values
    # isInput:      is input or output values
    def GetTestValues(self, isInput):
        return self.__getValues(False, isInput);
    
    ## 获得属性名称
    def GetFeatures(self, isInput):
        return self.__getFeatures(self.__trainSet, isInput)
    
    ## 获得属性名称
    def GetAllFeatures(self):
        ## 获得属性名称
        inFeatures = self.GetFeatures(True)
        outFeatures = self.GetFeatures(False)
        ## 属性合并
        return np.hstack((inFeatures, outFeatures))
    
    ## 创建文件夹
    def CreateDirectory(self, directPath):
        if not(os.path.exists(directPath)):
            os.makedirs(directPath)
    
    ## 读取数据
    def __setDataFrame(self):
        ## Read File
        if self.__trainFilePath != '': 
            self.__trainSet = pd.read_csv(self.__trainFilePath)
             ## Read File
        if self.__testFilePath != '': 
            self.__testSet = pd.read_csv(self.__testFilePath)
    
    ## 获得属性名称
    def __getFeatures(self, trainSet, isInput):
         ## 输入数据结束列号
        inEndColIndex = self.__startIndex + self.__inNum
        ## 输出起始列
        outStColIndex = inEndColIndex + self.__outSpacendex
        outEndColIndex = outStColIndex + self.__outNum
        # InputDatas
        if isInput:
             inFeatures = trainSet.columns[self.__startIndex : inEndColIndex]
             return inFeatures
        # OutputDatas
        else:
            outFeatures = trainSet.columns[outStColIndex : outEndColIndex]
            return outFeatures
    
    # Get csv Values
    # filePath:     csv File Path
    # isInput:      is input or output values
    def __getValues(self, isTrain, isInput):
        # Read CSV File
        trainSet = self.__trainSet if isTrain else self.__testSet
        ## 获得属性
        features = self.__getFeatures(trainSet, isInput)
        ## 值
        return trainSet[features].values





        